﻿using System.Collections;

/// <summary>
/// 游戏中
/// </summary>
public class Game_GamingState : GameState
{
	static Game_GamingState _inst;

	public static Game_GamingState inst {
		get {
			if (_inst == null) {
				_inst = new Game_GamingState ();
			}
			return _inst;
		}
	}


	public override void Enter ()
	{
		base.Enter ();

		DebugInfo.Log ("Enter Game_GamingState");

		if (GameDataKernel.inst.playing 
			&& !GameDataKernel.inst.GetPlayer(GameDataKernel.inst.playerIndex).ready) {
			UIGameWindow.inst.gameMaskLayerVisible = true;
		}
	}

	public override void Leave ()
	{
		DebugInfo.Log ("Leave Game_GamingState");

		base.Leave ();
	}

	public override bool ProcessPacket (IOMessage message)
	{
		switch (message.code) {
		case IOCmds.SMSG_PLAYER_JOIN_ROOM:
			if (message.state == IOStates.STATE_OK) {
				GameDataKernel.inst.HandleMessage (message);
			}
			return true;
		case IOCmds.SMSG_SET_PLAYER_CARDS:
			GameDataKernel.inst.HandleMessage (message);
			return true;

		case IOCmds.SMSG_SET_PLAY_CARD_PLAYER:
			GameDataKernel.inst.HandleMessage (message);
			return true;

		case IOCmds.SMSG_SET_CARD_NUM:
			GameDataKernel.inst.HandleMessage (message);
			return true;

		case IOCmds.SMSG_SET_PLAYED_CARD:
			UIRootController.inst.ShowMask (false);
			GameDataKernel.inst.HandleMessage (message);
			return true;

		case IOCmds.SMSG_PLAYER_PASS:
			UIRootController.inst.ShowMask (false);
			GameDataKernel.inst.HandleMessage (message);
			return true;

		case IOCmds.SMSG_SEND_NEW_CARD:
			GameDataKernel.inst.HandleMessage (message);
			return true;

		case IOCmds.SMSG_SET_ROUND_SETTEMENT:
			GameDataKernel.inst.HandleMessage (message);
			return true;

		case IOCmds.WS_CONN_CLOSED:
			if (_reconnecting) {
				NetIOController.inst.Connect (null);
			} else {
				// 游戏服务器掉线，重新连接
				UIGameReconnectWindow.Popup ();
			}
			return true;
		}
		return base.ProcessPacket (message);
	}
}
